* Replication of Tables and Figures related to NAFTA analysis

* Construct databases
cd "Empirics/AggregateData"
do "Database/CreateDatabase_NAFTA.do"
do "Database/NAFTA/OECD Import Share Growth for NAFTA.do"
cd "../MicroData"
do "dofiles/CreateDatabase_NAFTA_EIA.do"

* Generate Table A.11
cd "../AggregateData"
use "Database/output/Database_NAFTA.dta", clear
keep if sector>=3 & sector<=18 
drop year
egen ManuVA = total(VAshare)
gen VAshare2 = VAshare / ManuVA
drop ManuVA VAshare
ren VAshare2 VAshare
* Compute VAshare weighted averages
foreach x of var dt_mex dt2 dt_usa dt_io  { // dt
gen temp`x' = `x' * VAshare
egen `x'_manu = total(temp`x')
drop temp`x'
}
sort dt2
export excel Description dt_mex dt2 dt_usa dt_io using "../../Output/TableA11.xls", first(varl) sh("Main") replace
preserve 
keep dt_mex dt2 dt_usa dt_io
collapse (mean) dt_mex dt2 dt_usa dt_io
label var dt_mex "Change in Mexican Tariffs"
label var dt2 "Change in Mexican Input Tariffs"
label var dt_usa "Change in US Tariffs"
label var dt_io "Change in US Output Tariff"
export excel dt_mex dt2 dt_usa dt_io using "../../Output/TableA11.xls", first(varl) sh("Average") 
restore
preserve 
keep dt_mex dt2 dt_usa dt_io
collapse (median) dt_mex dt2 dt_usa dt_io
label var dt_mex "Change in Mexican Tariffs"
label var dt2 "Change in Mexican Input Tariffs"
label var dt_usa "Change in US Tariffs"
label var dt_io "Change in US Output Tariff"
export excel dt_mex dt2 dt_usa dt_io using "../../Output/TableA11.xls", first(varl) sh("Median") 
restore
preserve
keep dt_mex_manu dt2_manu dt_usa_manu dt_io_manu
collapse dt_mex_manu dt2_manu dt_usa_manu dt_io_manu 
label var dt_mex_manu "Change in Mexican Tariffs"
label var dt2_manu "Change in Mexican Input Tariffs"
label var dt_usa_manu "Change in US Tariffs"
label var dt_io_manu "Change in US Output Tariff"
export excel using "../../Output/TableA11.xls", first(varl) sh("Value-added weighted average")
restore

* Generate Table A.10 (column 2) and Figure A.11
cd "../MicroData"
use "temp/Database_NAFTA_EIA.dta", clear
merge 1:1 sector using "../AggregateData/Database/NAFTA/output/growthsi_OECD.dta"
drop _merge
gen growthsi2 = growthsi // growthsi2 gives the EIA import share growth except for Vehicles, where OECD data is used
replace growthsi2 = growthsi_OECD if sector==16
order sector growthsi growthsi2 growthsi_OECD
regress growthsi2 dt2 dt_io if dt2<0 ,r
estimates store One
label var growthsi2 "Growth in Import Share"
label var dt2 "Mexican Input Tariffs (2-digit)"
label var dt_io "Exposure to US Tariffs (2-digit)"
esttab One, keep(dt2 dt_io) b(%9.3f) se stats(r2 N) star(* 0.1 ** 0.05 *** 0.01)
esttab One using "../../Output/TableA10_col2.csv", label keep(dt2 dt_io) b(%9.2f) se stats(r2 N) star(* 0.1 ** 0.05 *** 0.01) replace

label var dt2 "Index of Change in Mexican Input Tariffs"
label var dt_io "Index of Exposure to US Tariff Changes"
twoway (scatter growthsi2 dt2 if dt2<0,  mlabel(Description_short)), graphregion(color(white)) legend(off) ytitle(Growth in Import Share) title(Imported Input Costs)
graph export "../../Output/FA11_A.pdf", replace
twoway (scatter growthsi2 dt_io if dt_io>-6,mlabel(Description_short) ), graphregion(color(white)) legend(off) ytitle(Growth in Import Share) title(Exposure to US Tariffs)
graph export "../../Output/FA11_B.pdf", replace

* Generate Table A.10 (column 1) and Figure A.10
use "temp/Database_NAFTA_EIA_4d", clear
label var gweighted "Growth in US Tariffs"
label var dweighted "Change in US Tariffs"
label var gweighted_mex "Growth in Mexican Tariffs"
label var dweighted_mex "Change in Mexican Tariffs"
_pctile growths, p(99)
local growths_cutoff=`r(r1)'
_pctile dweighted_mex, p(95)
local dweighted_mex_cutoff=`r(r1)'
_pctile dweighted, p(95)
local dweighted_cutoff=`r(r1)'	
twoway (scatter growths dweighted_mex if growths<`growths_cutoff' & dweighted_mex<`dweighted_mex_cutoff'), title(Mexican Tariffs) graphregion(color(white)) legend(off) ytitle(Growth in Import Share)
graph export "../../Output/FA10_A.pdf", replace
twoway (scatter growths dweighted if growths<`growths_cutoff' & dweighted<`dweighted_cutoff'	), title(US Tariffs) graphregion(color(white)) legend(off) ytitle(Growth in Import Share)
graph export "../../Output/FA10_B.pdf", replace
reg growths dweighted_mex dweighted, r
estimates store One
label var growths "Growth in Import Share"
label var dweighted_mex "Mexican Tariffs (4-digit)"
label var dweighted "US Tariffs (4-digit)"
esttab One, keep(dweighted_mex dweighted) b(%9.3f) se stats(r2 N) star(* 0.1 ** 0.05 *** 0.01)
esttab One using "../../Output/TableA10_col1.csv", label keep(dweighted_mex dweighted) b(%9.2f) se stats(r2 N) star(* 0.1 ** 0.05 *** 0.01) replace
cd ../..
